package leetcode;

import java.util.HashMap;
import java.util.LinkedList;

public class Lc496 {

    public int[] nextGreaterElement(int[] nums1, int[] nums2) {

        LinkedList<Integer> stack = new LinkedList<>();
        HashMap<Integer, Integer> map = new HashMap<>();

        for (int i = 0; i < nums2.length; i++) {
            if (stack.isEmpty() || stack.peek() > nums2[i]) {
                stack.push(nums2[i]);
            } else { //栈顶元素小于当前元素
                while (!stack.isEmpty() && stack.peek() < nums2[i]) {
                    map.put(stack.pop(), nums2[i]);
                }
                stack.push(nums2[i]);
            }
        }
        //留在栈里未能出栈的都是没有下一个更大元素的值
//        while (!stack.isEmpty()) {
//            map.put(stack.pop(), - 1);
//        }
        int[] ans = new int[nums1.length];
        for (int i = 0; i < nums1.length; i++) {
            if (map.containsKey(nums1[i])) {
                ans[i] = map.get(nums1[i]);
            } else ans[i] = -1;
        }
        return ans;
    }









}
